package cn.gdut.chengcai.base;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;

/**
 * 分页工具类
 */
public class PagerUtil {
	private static Pager getPager(Integer pageNo, Long itemCount) {
		Pager pager = new Pager(pageNo, ConstantUtil.PAGE_SIZE);
		pager.setItemCount(itemCount);
		return pager;
	}

	public static Pager getPager(HttpSession session, Long itemCount) {
		String pageNo = (String) session.getAttribute(ConstantUtil.STR_PAGE_NO);
		if (StringUtils.isEmpty(pageNo) == true) {
			pageNo = "1";
		}
		Pager pager = getPager(Integer.valueOf(pageNo), itemCount);
		session.setAttribute("pager", pager);
		return pager;
	}

	/*public static void fillVo(IBaseEntity entity, IBaseVo vo) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			BeanUtils.copyProperties(entity, vo);
			if (entity.getCreateTime() != null) {
				vo.setCreateTime(sdf.format(entity.getCreateTime()));
			}
			if (entity.getUpdateTime() != null) {
				vo.setUpdateTime(sdf.format(entity.getUpdateTime()));
			}
			vo.setData(entity);
		} catch (Exception e) {
	
		}
	}
	
	public static void fillEntity(IBaseVo vo, IBaseEntity entity) {
		try {
			Long id = entity.getId();
			String sn = entity.getSn();
			BeanUtils.copyProperties(vo, entity);
			entity.setId(id);
			entity.setSn(sn);
		} catch (Exception e) {
	
		}
	}
	
	public static void fillVoList(List list, List voList, String voClassName) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			List<IBaseEntity> entityList = list;
			for (IBaseEntity entity : entityList) {
				IBaseVo vo = (IBaseVo) ClassUtil.getObject(Class.forName(voClassName));
				BeanUtils.copyProperties(entity, vo);
				if (entity.getCreateTime() != null) {
					vo.setCreateTime(sdf.format(entity.getCreateTime()));
				}
				if (entity.getUpdateTime() != null) {
					vo.setUpdateTime(sdf.format(entity.getUpdateTime()));
				}
				vo.setData(entity);
				voList.add(vo);
			}
		} catch (Exception e) {
	
		}
	}
	
	*//**
		 * 填充ModelMap，主要包含数据列表对象list和页面数据对象pager
		 */
	/*
	private static void fillModelMap(HttpSession session, ModelMap map, List list, Pager pager) {
	if (map != null) {
		map.put("list", list);
	}
	
	if (session != null) {
		session.setAttribute("pager", pager);
	}
	}
	
	*//**
		 * 填充ModelMap，主要包含数据列表对象list和页面数据对象pager
		 */
	/*
	public static List fillModelMap(HttpSession session, ModelMap map, IBaseService service) {
	Long itemCount = service.countAll();
	Pager pager = PagerUtil.getPager(session, itemCount);
	List list = service.findByPage(pager.getPageNo(), pager.getPageSize());
	PagerUtil.fillModelMap(session, map, list, pager);
	return list;
	}
	
	*//**
		 * 填充ModelMap，主要包含数据列表对象list和页面数据对象pager
		 */
	/*
	public static List fillModelMapAsc(HttpSession session, ModelMap map, IBaseService service) {
	Long itemCount = service.countAll();
	Pager pager = PagerUtil.getPager(session, itemCount);
	List list = service.findByPageAsc(pager.getPageNo(), pager.getPageSize());
	PagerUtil.fillModelMap(session, map, list, pager);
	return list;
	}
	
	*//**
		 * 填充ModelMap，主要包含数据列表对象list和页面数据对象pager
		 */
	/*
	public static void fillModelMapWithVoList(HttpSession session, ModelMap map, List list, List voList, String voClassName) {
	Long itemCount = (long) list.size();
	Pager pager = PagerUtil.getPager(session, itemCount);
	fillVoList(list, voList, voClassName);
	PagerUtil.fillModelMap(session, map, voList, pager);
	}
	
	*//**
		 * 填充ModelMap，主要包含数据列表对象list和页面数据对象pager
		 */
	/*
	public static List fillModelMapWithVoList(HttpSession session, ModelMap map, Pager pager, List voList, String voClassName) {
	// Pager pager = PagerUtil.getPager(request, itemCount);
	fillVoList(pager.getData(), voList, voClassName);
	PagerUtil.fillModelMap(session, map, voList, pager);
	return voList;
	}
	
	*//**
		 * 填充ModelMap，主要包含数据列表对象list和页面数据对象pager
		 */
	/*
	public static List fillModelMapWithVoList(HttpSession session, ModelMap map, IBaseService service, List voList, String voClassName) {
	Long itemCount = service.countAll();
	Pager pager = PagerUtil.getPager(session, itemCount);
	List<IBaseEntity> list = service.findByPage(pager.getPageNo(), pager.getPageSize());
	fillVoList(list, voList, voClassName);
	PagerUtil.fillModelMap(session, map, voList, pager);
	return voList;
	}
	
	*//**
		 * 填充ModelMap，主要包含数据列表对象list和页面数据对象pager
		 */
	/*
	public static List fillModelMapWithVoListNoPager(HttpSession session, ModelMap map, IBaseService service, List voList, String voClassName) {
	Long itemCount = service.countAll();
	Pager pager = PagerUtil.getPager(session, itemCount);
	List<IBaseEntity> list = service.findAll();
	fillVoList(list, voList, voClassName);
	PagerUtil.fillModelMap(session, map, voList, pager);
	return voList;
	}
	
	*//**
		 * 填充ModelMap，主要包含数据列表对象list和页面数据对象pager
		 */
	/*
	public static List fillModelMapByCondition(HttpSession session, ModelMap map, IBaseService service, IBaseEntity condition) {
	Long itemCount = service.countByCondition(condition);
	Pager pager = PagerUtil.getPager(session, itemCount);
	List list = service.findByCondition(condition, pager.getPageNo(), pager.getPageSize());
	PagerUtil.fillModelMap(session, map, list, pager);
	return list;
	}
	
	*//**
		 * 填充ModelMap，主要包含数据列表对象list和页面数据对象pager
		 */
	/*
	public static List fillModelMapByConditionAsc(HttpSession session, ModelMap map, IBaseService service, IBaseEntity condition) {
	Long itemCount = service.countByCondition(condition);
	Pager pager = PagerUtil.getPager(session, itemCount);
	List list = service.findByConditionAsc(condition, pager.getPageNo(), pager.getPageSize());
	PagerUtil.fillModelMap(session, map, list, pager);
	return list;
	}
	
	public static void fillModelMapByConditionWithVoList(HttpSession session, ModelMap map, IBaseService service, IBaseEntity condition, List voList, String voClassName) {
	Long itemCount = service.countByCondition(condition);
	Pager pager = PagerUtil.getPager(session, itemCount);
	List<IBaseEntity> list = service.findByCondition(condition, pager.getPageNo(), pager.getPageSize());
	fillVoList(list, voList, voClassName);
	PagerUtil.fillModelMap(session, map, voList, pager);
	}
	
	public static void fillModelMapByConditionWithVoListAsc(HttpSession session, ModelMap map, IBaseService service, IBaseEntity condition, List voList, String voClassName) {
	Long itemCount = service.countByCondition(condition);
	Pager pager = PagerUtil.getPager(session, itemCount);
	List<IBaseEntity> list = service.findByConditionAsc(condition, pager.getPageNo(), pager.getPageSize());
	fillVoList(list, voList, voClassName);
	PagerUtil.fillModelMap(session, map, voList, pager);
	}
	
	public static void fillModelMapByConditionCustomWithVoList(HttpSession session, ModelMap map, IBaseService service, IBaseEntity condition, List voList, String voClassName, String sortString) {
	Long itemCount = service.countByCondition(condition);
	Pager pager = PagerUtil.getPager(session, itemCount);
	List<IBaseEntity> list = service.findByConditionCustom(condition, pager.getPageNo(), pager.getPageSize(), sortString);
	fillVoList(list, voList, voClassName);
	PagerUtil.fillModelMap(session, map, voList, pager);
	}
	
	public static void fillModelMapByConditionCustomWithVoListNoPager(HttpSession session, ModelMap map, IBaseService service, IBaseEntity condition, List voList, String voClassName, String sortString) {
	List<IBaseEntity> list = service.findByConditionCustom(condition, sortString);
	fillVoList(list, voList, voClassName);
	map.put("list", voList);
	}
	
	public static void fillModelMapByConditionWithVoListNoPager(ModelMap map, IBaseService service, IBaseEntity condition, List voList, String voClassName) {
	List<IBaseEntity> list = service.findByConditionAsc(condition);
	fillVoList(list, voList, voClassName);
	map.put("list", voList);
	}
	
	*//**
		 * 填充ModelMap，主要包含数据列表对象list和页面数据对象pager
		 */
	/*
	public static void fillModelMapWithVoEntity(ModelMap map, Long id, IBaseService service, IBaseVo vo) {
	IBaseEntity entity = service.findById(id);
	BeanUtils.copyProperties(entity, vo);
	
	// vo属性填充
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	if (entity.getCreateTime() != null) {
		vo.setCreateTime(sdf.format(entity.getCreateTime()));
	}
	if (entity.getUpdateTime() != null) {
		vo.setUpdateTime(sdf.format(entity.getUpdateTime()));
	}
	vo.setData(entity);
	
	map.put("entity", vo);
	}
	
	*//**
		 * 清除voList中的vo的Data
		 *//*
		public static void cleanVoListData(List voList) {
		for (Object vo : voList) {
			((IBaseVo) vo).setData(null);
		}
		}*/

	/*
	 * public static void fillModelMapWithVoList(HttpServletRequest request,
	 * ModelMap map, ScheduleJobService service, List<ScheduleJobVo> voList,
	 * String name) { // TODO Auto-generated method stub
	 * 
	 * }
	 */
}
